package com.nativetalk.common.util;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.common.Constant;
import com.nativetalk.common.getui.AndroidStudentGetui;
import com.nativetalk.common.getui.IosStudentGetui;
import com.nativetalk.dao.TdNtLiveDao;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/1/7.
 */
public class CallStudent extends Thread{
    private static Logger logger = Logger.getLogger(CallStudent.class);
    public void run(TdNtLiveDao tdNtLiveDao, int sst_id,
                    String tea_name, String tea_head_portrait, String cou_name, TdNtMember tdNtMember, String token, io.netty.channel.Channel channel,int surplus_time)
    {
        RetInfo retInfo = new RetInfo();
        retInfo.setMark("0");
        retInfo.setTip("stt_id");
        Map<String, Object> result = new HashMap<>();
        result.put("type","estimateTime");
        result.put("sst_id",sst_id);
        retInfo.setObj(result);
        channel.writeAndFlush(BeanToJson.RetInfoToJson(retInfo));

        if (MemcachedUtils.get(tdNtMember.getToken()) != null) {
            if (tdNtMember.getEqu_client_id() != null && !(tdNtMember.getEqu_client_id().equals("1212313fasdf"))) {
                if (tdNtMember.getEqu_type().equals("1")) {
                    AndroidStudentGetui.apnpush("外教君","One teacher sent you a teaching request, check now! ",
                            tdNtMember.getEqu_client_id(),sst_id,tdNtMember.getMemb_name(),tdNtMember.getMemb_head_portrait()
                            ,token + sst_id,tea_head_portrait,tea_name,cou_name,surplus_time);
                } else {
                    logger.info(tdNtMember.getEqu_client_id());
                    IosStudentGetui.apnpush("外教君","One teacher sent you a teaching request, check now! ",
                            tdNtMember.getEqu_client_id(),sst_id,tdNtMember.getMemb_name(),tdNtMember.getMemb_head_portrait()
                            ,token + sst_id,tea_head_portrait,tea_name,cou_name,surplus_time);

                }

            }


        }
        logger.info("tea:"+ Constant.teacher_appId);
        int i = 0;
        while(true){
            TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtLiveDao.findTdNtStudyTeachUpdate(sst_id);
            try {
                i++;
                sleep(1000);
                //logger.info(i);
                //这里可以写你自己要运行的逻辑代码   0找寻老师中 1学习中 2待支付 3待评价 4已完成 5已完成 6已取消
                if ( !(tdNtStudyTeachDetail.getStudy_state().equals("0")) || i >= 120 ) {
                    if (tdNtStudyTeachDetail.getStudy_number().equals("6")) {
                        channel.close();
                    }
                    break;
                }


            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}
